package com.GamersCom.games;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.GamersCom.db.Game;
import com.GamersCom.db.Genre;
import com.GamersCom.db.Platform;
import com.GamersCom.model.GameMgr;
import com.GamersCom.model.GenreMgr;
import com.GamersCom.model.MgrFactory;
import com.GamersCom.model.PlatformMgr;

/**
 * Servlet implementation class AdminGameDetail
 */
public class AdminGameDetail extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		
		String nextView = "/adminGameDetails.jsp";
		MgrFactory factory = (MgrFactory) this.getServletContext()
				.getAttribute("mgrFactory");
		PlatformMgr pfMgr = factory.getPlatformMgr();
		GenreMgr genreMgr = factory.getGenreMgr();
		GameMgr gameMgr = factory.getGameMgr();

		// retrieve the gameId
		String id = (String) request.getAttribute("gameId");
		//remove the gameId 
		request.removeAttribute("gameId");

		if (id == null)
		{
			nextView = "/result.jsp";
			request.setAttribute("error", "404 Page Not Found");
		}
		else
		{
			int gid = Integer.parseInt(id);
			try
			{
				List<Platform> pfList = pfMgr.getAll();
				List<Genre> genreList = genreMgr.getAll();
				Game game = gameMgr.getGameById(gid);
				
				
				if ( game == null )  // gid is not found in db
				{
					nextView = "/result.jsp";
					request.setAttribute("error", "404 Page Not Found");
				}
				else
				{
					request.setAttribute("pfList", pfList);
					request.setAttribute("genreList", genreList);
					request.setAttribute("game", game);	
				}
				
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
				
			}

		}
		request.getRequestDispatcher(nextView).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		String nextView;

		MgrFactory factory = (MgrFactory) this.getServletContext()
				.getAttribute("mgrFactory");
		PlatformMgr pfMgr = factory.getPlatformMgr();
		GenreMgr genreMgr = factory.getGenreMgr();
		GameMgr gameMgr = factory.getGameMgr();

		
		String submit = request.getParameter("submit");  // get submit value		
	
		//Do update action
		if ( submit.equals("Update")) 
		{
			
			//retrieve the form data
			String id = request.getParameter("id");
			String name = request.getParameter("name");
			String company = request.getParameter("company");
			String website = request.getParameter("website");
			String release = request.getParameter("releaseDate");
			String summary = request.getParameter("summary");
//			String platform = request.getParameter("platform");
//			String genre = request.getParameter("genre");
			String platform = request.getParameter("platform");
			String genre = request.getParameter("genre");

	for ( Object s : request.getParameterMap().keySet() )
	{
		System.out.println(s.toString() + " " + request.getParameter(s.toString()) );
	}
			
			
			Game game = new Game();
			game.setId(Integer.parseInt(id));
			game.setName(name);
			game.setCompany(company);
			game.setWebsite(website);
			game.setSummary(summary);
//			game.setReleaseDate(releaseDate);
			game.setPlatform(platform);
			game.setGenre(genre);
			
			//validate the form data
			
			//update the game
			try
			{
				gameMgr.update(game);
				
				// redirect to ajax response
				if ( request.getParameter("ajax") == null )
				{
					request.setAttribute("msg", "Update Successfully!");
					nextView = "/adminGameDetails.jsp";
					doGet(request,response);
				}
				else
				{
					request.setAttribute("msg", "SUCCESS");	
					nextView = "/ajaxResult.jsp";						
					request.getRequestDispatcher(nextView).forward(request, response);
				}
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		
//		request.getRequestDispatcher(nextView).forward(request, response);
	}

}
